﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;

namespace MattKane.Modules.YouTubeGallery
{
    public partial class Settings : ModuleSettingsBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    if (ModuleSettings["Width"] != null)
                    {
                        tbWidth.Text = (string)ModuleSettings["Width"];
                    }

                    if (ModuleSettings["Height"] != null)
                    {
                        tbHeight.Text = (string)ModuleSettings["Height"];
                    }


                    if (ModuleSettings["OutlineColor"] != null)
                    {
                        tbOutlineColor.Text = (string)ModuleSettings["OutlineColor"];
                    }


                    if (ModuleSettings["BackgroundColor"] != null)
                    {
                        tbBackgroundColor.Text = (string)ModuleSettings["BackgroundColor"];
                    }

                    if ((string)ModuleSettings["Autoplay"] == true.ToString())
                    {
                        cbAutoplay.Checked = true;
                    }
                    else
                    {
                        cbAutoplay.Checked = false;
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                ModuleController objModules = new ModuleController();
                objModules.UpdateModuleSetting(ModuleId, "Width", tbWidth.Text);
                objModules.UpdateModuleSetting(ModuleId, "Height", tbHeight.Text);
                objModules.UpdateModuleSetting(ModuleId, "OutlineColor", tbOutlineColor.Text);
                objModules.UpdateModuleSetting(ModuleId, "BackgroundColor", tbBackgroundColor.Text);
                objModules.UpdateModuleSetting(ModuleId, "Autoplay", cbAutoplay.Checked.ToString());

                ModuleController.SynchronizeModule(ModuleId);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
    }
}